package component.fulltextsearch.index.handlingtypes;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class PlainTextHandler implements IInputStreamHandler {

	public String getText(InputStream is) throws InputStreamHandlerException {

		String bodyText = "";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(is));
			String line = null;
			while ((line = br.readLine()) != null) {
				bodyText += line;
			}
		} catch (IOException e) {
			throw new InputStreamHandlerException("Cannot read the text document",e);
		} finally {			
			try {
				if(br!=null) br.close();
			} catch (IOException e) {	
				e.printStackTrace();
			}
		}
		return bodyText;		
	}

	public static void main(String[] args) throws Exception {
		String file = "D:/opt/test.txt";
		PlainTextHandler handler = new PlainTextHandler();
		String doc = handler.getText(new FileInputStream(new File(file)));
		System.out.println(doc);
	}
}
